home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / awe2-0_1.lha / awe2-0.1 / Src / SampleStatistic.cc < prev    next >
C/C++ Source or Header  |  1990-03-15  |  4KB  |  178 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Dirk Grunwald (grunwald@cs.uiuc.edu)
  5.  
  6. This file is part of GNU CC.
  7.  
  8. GNU CC is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY.  No author or distributor
  10. accepts responsibility to anyone for the consequences of using it
  11. or for whether it serves any particular purpose or works at all,
  12. unless he says so in writing.  Refer to the GNU CC General Public
  13. License for full details.
  14.  
  15. Everyone is granted permission to copy, modify and redistribute
  16. GNU CC, but only under the conditions described in the
  17. GNU CC General Public License.   A copy of this license is
  18. supposed to have been given to you along with GNU CC so you
  19. can know your rights and responsibilities.  It should be in a
  20. file named COPYING.  Among other things, the copyright notice
  21. and this notice must be preserved on all copies.  
  22. */
  23. #include <stream.h>
  24. #include "SampleStatistic.h"
  25. #include <math.h>
  26.  
  27. // error handling
  28.  
  29. void default_SampleStatistic_error_handler(char* msg)
  30. {
  31.   cerr << "Fatal SampleStatistic error. " << msg << "\n";
  32.   exit(1);
  33. }
  34.  
  35. //one_arg_error_handler_t SampleStatistic_error_handler = default_SampleStatistic_error_handler;
  36.  
  37. //    one_arg_error_handler_t set_SampleStatistic_error_handler(one_arg_error_handler_t f)
  38. //    {
  39. //      one_arg_error_handler_t old = SampleStatistic_error_handler;
  40. //      SampleStatistic_error_handler = f;
  41. //      return old;
  42. //    }
  43. //    
  44.  
  45. void SampleStatistic::error(char* msg)
  46. {
  47. //  (*SampleStatistic_error_handler)(msg);
  48.     default_SampleStatistic_error_handler(msg);
  49. }
  50.  
  51. void
  52. SampleStatistic::reset()
  53. {
  54.     n = 0; x = x2 = 0.0;
  55.     maxValue = -MAXFLOAT;
  56.     minValue = MAXFLOAT;
  57. }
  58.  
  59. void
  60. SampleStatistic::operator+=(double value)
  61. {
  62.     n ++;
  63.     x += value;
  64.     x2 += (value * value);
  65.     if ( minValue > value) minValue = value;
  66.     if ( maxValue < value) maxValue = value;
  67. }
  68.  
  69. double
  70. SampleStatistic::mean()
  71. {
  72.     if ( n > 0) {
  73.     return (x / n);
  74.     }
  75.     else {
  76.     return ( 0.0 );
  77.     }
  78. }
  79.  
  80. double
  81. SampleStatistic::var()
  82. {
  83.     if ( n > 1) {
  84.     return(( x2 - ((x * x) /  n)) / ( n - 1));
  85.     }
  86.     else {
  87.     return ( 0.0 );
  88.     }
  89. }
  90.  
  91. double
  92. SampleStatistic::stdDev()
  93. {
  94.     if ( n <= 0 || this -> var() <= 0) {
  95.     return(0);
  96.     } else {
  97.     return( (double) sqrt( var() ) );
  98.     }
  99. }
  100.  
  101. int
  102. SampleStatistic::samples()
  103. {
  104.     return(n);
  105. }
  106.  
  107. double
  108. SampleStatistic::min()
  109. {
  110.     return(minValue);
  111. }
  112.  
  113. double
  114. SampleStatistic::max()
  115. {
  116.     return(maxValue);
  117. }
  118.  
  119. double
  120. SampleStatistic::sum()
  121. {
  122.     return(x);
  123. }
  124.  
  125. // t-distribution: given p-value and degrees of freedom, return t-value
  126. // adapted from Peizer & Pratt JASA, vol63, p1416
  127.  
  128. double
  129. SampleStatistic::tval(double p, int df) 
  130. {
  131.   double t;
  132.   int positive = p >= 0.5;
  133.   p = (positive)? 1.0 - p : p;
  134.   if (p <= 0.0 || df == 0)
  135.     t = HUGE;
  136.   else if (p == 0.5)
  137.     t = 0.0;
  138.   else if (df == 1)
  139.     t = 1.0 / tan((p + p) * 1.57079633);
  140.   else if (df == 2)
  141.     t = sqrt(1.0 / ((p + p) * (1.0 - p)) - 2.0);
  142.   else
  143.   {    
  144.     double ddf = df;
  145.     double a = sqrt(log(1.0 / (p * p)));
  146.     double aa = a * a;
  147.     a = a - ((2.515517 + (0.802853 * a) + (0.010328 * aa)) /
  148.              (1.0 + (1.432788 * a) + (0.189269 * aa) +
  149.               (0.001308 * aa * a)));
  150.     t = ddf - 0.666666667 + 1.0 / (10.0 * ddf);
  151.     t = sqrt(ddf * (exp(a * a * (ddf - 0.833333333) / (t * t)) - 1.0));
  152.   }
  153.   return (positive)? t : -t;
  154. }
  155.  
  156. double
  157. SampleStatistic::confidence(int interval)
  158. {
  159.     int samp = samples() - 1;
  160.     double t = tval(double(100 + interval) * 0.005, samp );
  161.  
  162.     if (t == HUGE)
  163.     return t;
  164.     else
  165.     return (t * stdDev()) / sqrt(double( samp ));
  166. }
  167.  
  168. double
  169. SampleStatistic::confidence(double p_value)
  170. {
  171.     int samp = samples() - 1;
  172.     double t = tval((1.0 + p_value) * 0.5, samp);
  173.     if (t == HUGE)
  174.     return t;
  175.     else
  176.     return (t * stdDev()) / sqrt(double( samp ));
  177. }
  178.